package com.android.task5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.drawable.shapes.ArcShape;
import android.util.Log;

public class PanoramioManager {
	
	private final static String INFO_TAG = "Panoramio Manager Info";


	private PanoramioRequest mPanoramioRequest;
	private PanoramioJSONResponse mPanoramioResponse;
	private ArrayList<PanoramioPhoto> mPanoramioPhotos;
	
	public PanoramioManager(){
		mPanoramioRequest = new PanoramioRequest();
		mPanoramioResponse = new PanoramioJSONResponse();
		mPanoramioPhotos = new ArrayList<PanoramioPhoto>();
			}
	
	public void getJSONResponseWithinBoundingBox(BoundingBox bbox){
		
		try {
			mPanoramioRequest.createStringURLforBoundingBox(bbox);
			mPanoramioRequest.getInfo();
			URL newUrl = new URL(mPanoramioRequest.getPanoramioRESTSearchURL());
			URLConnection urlConn = newUrl.openConnection();
			InputStream is = urlConn.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			final StringBuffer sb = new StringBuffer();
			String line;
			while ((line = br.readLine()) != null)
			{
			sb.append(line);
			}
			br.close();
			String data = sb.toString();
			Log.i("data", "received data:" + data);
//			JSONobj = new JSONObject(data);
			
			mPanoramioResponse.parseJSONResponse(data);
			mPanoramioPhotos = mPanoramioResponse.getPanoramioPhotos();
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public ArrayList<PanoramioPhoto> getPanoramioPhotos(){
		return mPanoramioPhotos;
	}
}
